package cn.com.czpb.client.module.news.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.MToolBarActivity;

/**
 * Created by 猪猪侠 on 2017/11/16.
 */

public class CircleActivity extends MToolBarActivity {
    @BindView(R.id.webview_id)
    WebView webView;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @Override
    public void initView() {
        super.initView();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_circle;
    }

    @Override
    public void initData() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
//        webView.loadUrl("startAppSettings");
        webView.loadUrl(InterfaceJsonfile.CIRCLEURL+spu.getUser().getUid());
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new MyWebChromeClient());
    }
    public class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
            final JsResult finalJsResult = jsResult;
            new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finalJsResult.confirm();
                }
            }).setCancelable(false).create().show();
            return true;
        }
    }
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
        webView.goBack();
        }else{
            finish();
        }
        super.onBackPressed();
    }
}
